using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Mapping;
using SolanumSolstice.Buildings;

namespace SolanumSolstice.Units
{

    public class Zombie : Unit
    {
        protected bool pursuingHuman=false;

        //#region accessors

        //#endregion

        public Zombie(ContentManager content, Vector2 position)
            : base(content, position)
        {
            base.isZombie = true;
            sightRadius = 6 * 32;
            //this.SetTarget(Map.getRandomWalkableSpace());
        }


        public override void Update(GameTime gameTime, List<Unit> enemiesInRange, List<Unit> friendsInRange)
        {
                // (already have Base Code)
               //   base.Update();

            //Check Recovery Time from actions
            TimeSpan elapsed = gameTime.ElapsedGameTime;

            recoveryTime -= elapsed.Milliseconds / 1000.0f;


            if (recoveryTime <= 0)
            {
                recoveryTime = 0;
                actionReady = true;
            }

            if (inBuilding == false)
            {
                List<Unit> enemiesInSight = SolanumSolstice.FindEnemyUnits(this, sightRadius);
                if (enemiesInSight.Count > 0 && enemiesInRange.Count == 0 && pursuingHuman == false)
                {
                    pursuingHuman = true;
                    SolanumSolstice.soundBank.PlayCue("zombiegrowl");
                    SetTarget(enemiesInSight[0].Position + new Vector2(16, 16));
                }

                //Attack if enemy target is in range
                if (enemiesInRange.Count > 0)
                {
                    isAttacking = true;

                    if (actionReady == true)
                    {
                        int atkIndex = rand.Next(enemiesInRange.Count - 1);

                        this.DealDamage(enemiesInRange[atkIndex]);

                        direction = enemiesInRange[atkIndex].Position - position;

                        direction.Normalize();


                        recoveryTime = weapon.RecoveryTime;
                        initialRecoveryTime = weapon.RecoveryTime;

                        actionReady = false;
                    }

                }
                else if (SolanumSolstice.IsCollidingWithRoadBlock(this) != null)
                {
                    isAttacking = true;

                    if (actionReady == true)
                    {
                        RoadBlock roadBlock = SolanumSolstice.IsCollidingWithRoadBlock(this);

                        roadBlock.TakeDamage(rand.Next(Weapon.AttackPower) + attackPower);
                        if (roadBlock.Damage > 0)
                        {
                            SolanumSolstice.soundBank.PlayCue("electrocute");
                            this.TakeDamage(roadBlock.Damage);
                        }

                        recoveryTime = weapon.RecoveryTime;
                        initialRecoveryTime = weapon.RecoveryTime;

                        actionReady = false;
                    }
                }
                else
                {
                    Trap test = SolanumSolstice.IsCollidingWithTrap(this);
                    if (test != null)
                    {
                        TakeDamage(hitPoints);
                        test.UseCount--;
                    }
                    else
                    {
                        if (isAttacking == true)
                        {

                            //targetReached = true;
                            SetTarget(Map.getRandomWalkableSpace(position, sightRadius * 2));

                
                        }

                        isAttacking = false;
                    }




                    //Move if target is not reached
                    if (!targetReached && isAttacking == false)
                    {
                        position += velocity * direction;
                    }



                    //If Target Reached, Set Next Target or Stop
                    if (Math.Abs(position.X - targetPosition.X) < velocity
                       && Math.Abs(position.Y - targetPosition.Y) < velocity)
                    {
                        targetReached = true;


                        if (pathIndex > 0 && path != null)
                        {
                            pathIndex--;
                            targetPosition = path.GetPath[pathIndex].Position + groupOffset;
                            direction = targetPosition - position;
                            direction.Normalize();
                            targetReached = false;
                        }
                        else
                        {
                            
                            //Vector2 target = SolanumSolstice.GetNearestHumanEntity(this);
                            if (pursuingHuman == true && path != null)
                            {
                                pursuingHuman = false;
                            }
                            else
                            {
                                SetTarget(Map.getRandomWalkableSpace(position, sightRadius * 2));

                            }
                        }  //end else
                    }
                }
            }

            


        }

    }

}


